import Const from "../Const";
import { ui } from "../ui/layaMaxUI";
import Actor from "./Actor";
import ActorV from "./ActorV";
import BattleManager from "./BattleManager";
import BattleModel from "./BattleModel";

export default class BattleView extends ui.view.BattleViewUI{
    constructor(){
        super()
        this.width=Laya.stage.width
        this.height=Laya.stage.height
        Laya.stage.addChild(this)
    }
    battleModel:BattleModel
    prefab_actorV:Laya.Prefab

    actorVDic={} 


    init(battleModel:BattleModel){
        this.prefab_actorV=Laya.loader.getRes('prefab/actorV.prefab') as Laya.Prefab

        //根据model来初始化所有对象
        this.battleModel=battleModel

        this.battleModel.teams.forEach(team=>{
            team.actors.forEach(actor=>{
                if(actor){
                    let go=this.prefab_actorV.create() as Laya.Sprite
                    this.itemPlace.addChild(go)
                    let actorV=go.getComponent(ActorV) as ActorV
                    actorV.gId=actor.gId
                    actorV.setAtk(actor.atk)
                    actorV.setHp(actor.hp)
                    let pos=this.getPos(actor.teamId,actor.order)
                    actorV.spr.pos(pos.x,pos.y)
                    if(team.id===1){
                        actorV.setFlip(true)
                    }
                    this.actorVDic[actor.gId]=actorV
                }
            })
        })


        //按钮
        this.btn_battle.on(Laya.Event.CLICK,this,()=>{
            BattleManager.I.startBattle()
        })
    }


    getPos(teamId,order):{x,y}{
        if(teamId===0){
            let w=this.team0Area.width/Const.MAX_ACTOR
            let x=this.team0Area.x+w*order+w/2
            let y=this.team0Area.y+this.team0Area.height/2
            return {x,y}
        }else{
            let w=this.team1Area.width/Const.MAX_ACTOR
            let x=this.team1Area.x+w*(Const.MAX_ACTOR-order)+w/2
            let y=this.team1Area.y+this.team1Area.height/2
            return {x,y}
        }
    }

    getActorV(gId):ActorV{
        return this.actorVDic[gId]
    }
}